<body>
    <h2>咨询途径</h2>

    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <div id="dlg" class="easyui-dialog" style="width:500px;height:400px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">添加途径</div>
        <form id="fm" method="post" novalidate>
        	<input name="id" type="hidden">
            <div class="fitem">
                <label>名称:</label>
                <input name="name" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>描述:</label>
                <textarea name="description" style="height:60px;width:70%;float:left"></textarea>
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="consultWayJs.save()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
    </div>
	
    <script type="text/javascript">
(function(consultWayJs, $, undefined ) {    
	$('#list').datagrid({
		title:'咨询途径',
		iconCls:'icon-save',
		height:400,
		fitColumns:true,
		singleSelect:true,
		url:null,
		frozenColumns:[[    
	                    {title:'ID',field:'id',width:80,sortable:true}  
	                ]],  
	    columns:[[
	             	{title:'名称',field:'name',width:80},
	             	{title:'描述',field:'description',width:150}
	             ]],
		pagination:false,
		toolbar:[{  
            id:'btnadd',  
            text:'添加',  
            iconCls:'icon-add',  
            handler:newItem
        },{  
            id:'btnupdate',  
            text:'修改',  
            iconCls:'icon-edit',  
            handler:edit
        },'-',{  
            id:'btndelete',  
            text:'删除',   
            iconCls:'icon-remove',
            handler:destroy
        },{
        	id:'btnrefresh',
        	text:'刷新',
        	iconCls:'icon-reload',
        	handler:refresh
        }]
	});
	
	consultWayJs.init= function(){
		
		refresh();
	}
	
	consultWayJs.loadItemData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	function simpleResultHanding(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	$.messager.show({
                title: 'success',
                msg: "success"
            });
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	}
	
    var url;
    function newItem(){
        $('#dlg').dialog('open').dialog('setTitle','添加咨询途径');
        $('#fm').form('clear');
        url = 'dispatch/manage/consultway/add';
    }
    function refresh(){
    	workbenchJs.loadData("dispatch/manage/consultway/all",consultWayJs.loadItemData,"查询咨询途径");
    }
    function edit(){
        var row = $('#list').datagrid('getSelected');
        if (row){
            $('#dlg').dialog('open').dialog('setTitle','编辑咨询途径');
            $('#fm').form('clear');
            $('#fm').form('load',row);
            url = 'dispatch/manage/consultway/update?id='+row.id;
        }else
        {
        	$.messager.alert("Error",'请先选择一条记录');
        }
    }
    consultWayJs.save = function(){
        $('#fm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result = eval('('+result+')');
            	var res = result.result;
                if(res == "SUCCESS")
                {
                	$.messager.show({
                        title: 'success',
                        msg: "success"
                    });
                	refresh();
                }else if(res == "COMPONENT_ERROR")
                {
                	$.messager.alert("Error",result.payload.value);
                }else
                {
                	$.messager.alert("Error",result.result);
                }
            }
        });
        $('#dlg').dialog('close');
    }
   
    function destroy(){
        var row = $('#list').datagrid('getSelected');
        if (row){
        	workbenchJs.loadData("dispatch/manage/consultway/delete?id="+row.id,consultWayJs.deleteItemResult,"删除咨询途径");
        }else
        	{
        		$.messager.alert("Error",'请先选择一条记录');
        	}
    }

    consultWayJs.deleteItemResult = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	refresh();
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
        refresh();
    };
}( window.consultWayJs = window.consultWayJs || {}, jQuery ));

$(function(){
	consultWayJs.init();
});
    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
            float:left;
        }
        .view{
        	margin-bottom:5px;
        	margin-left:9px;
        	display:inline-block;
            width:200px;
        }
    </style>